﻿Imports System.Data.OleDb
Imports BUS
Imports DTO

Public Class frmTuyenXe

    Dim dtTUYEN As DataTable
    Dim vtClick As Integer = 0

    Private Sub TuyenXe_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            dgvTuyen.DataSource = TuyenBus.LayDanhSachTuyen()
            Dim tuyen As TuyenDto = LayTuyenTu_DataGridView(0)
            HienThiThongTinTUYEN(tuyen)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub
    Public Function KT_NhapLieu_Them() As String
        Dim str As String = ""
        If txtMaTuyen.Text <> "" AndAlso Not IsNumeric(txtMaTuyen.Text) Then
            str = "Mã tuyến là số!!!"
            txtMaTuyen.Focus()
        ElseIf txtTenTuyen.Text = "" Then
            str = "Tên tuyến không được rỗng!!!"
            txtTenTuyen.Focus()
        ElseIf txtDiaDiemDi.Text = "" Then
            str = "Diem diem di không được rỗng!!!"
            txtDiaDiemDi.Focus()
        ElseIf txtDiaDiemDen.Text = "" Then
            str = "Diem diem den không được rỗng!!!"
            txtDiaDiemDen.Focus()
        End If
        Return str
    End Function

    Public Function LayTuyenTuTextboxs() As TuyenDto
        Dim tuyen As New TuyenDto
        If IsNumeric(txtMaTuyen.Text) Then
            tuyen.MaTuyenXe = txtMaTuyen.Text
        End If
        tuyen.TenTuyenXe = txtTenTuyen.Text
        tuyen.DiaDiemDi = txtDiaDiemDi.Text
        tuyen.DiaDiemDen = txtDiaDiemDen.Text

        Return tuyen
    End Function

    Public Function LayTuyenTu_DataGridView(ByVal vtdong As Integer) As TuyenDto
        Dim tuyen As New TuyenDto
        tuyen.MaTuyenXe = dgvTuyen.Rows(vtdong).Cells("MaTuyen").Value
        tuyen.TenTuyenXe = dgvTuyen.Rows(vtdong).Cells("TenTuyen").Value
        tuyen.DiaDiemDi = dgvTuyen.Rows(vtdong).Cells("DiaDiemDi").Value
        tuyen.DiaDiemDen = dgvTuyen.Rows(vtdong).Cells("DiaDiemDen").Value
        Return tuyen
    End Function
    Public Sub HienThiThongTinTUYEN(ByVal tuyen As TuyenDto)
        txtMaTuyen.Text = tuyen.MaTuyenXe
        txtTenTuyen.Text = tuyen.TenTuyenXe
        txtDiaDiemDi.Text = tuyen.DiaDiemDi
        txtDiaDiemDen.Text = tuyen.DiaDiemDen
    End Sub
    Public Sub ResetTextboxs()
        txtMaTuyen.Text = ""
        txtTenTuyen.Text = ""
        txtDiaDiemDi.Text = ""
        txtDiaDiemDen.Text = ""
    End Sub
    Private Sub btnTimTuyen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimTuyen.Click
        dgvTuyen.DataSource = TuyenBus.TimTuyen(Me.LayTuyenTuTextboxs())
    End Sub
    Private Sub btnThemTuyen_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemTuyen.Click
        Dim ktNhapLieu As String = KT_NhapLieu_Them()
        If (ktNhapLieu <> "") Then
            MessageBox.Show(ktNhapLieu, "THÔNG BÁO")
            Exit Sub
        End If

        Dim kq As Integer = TuyenBus.ThemTuyen(Me.LayTuyenTuTextboxs())
        If (kq = 0) Then
            MessageBox.Show("Tên tuyến xe đã có trong dữ liệu!!!", "THÔNG BÁO")
        Else
            dgvTuyen.DataSource = TuyenBus.LayDanhSachTuyen()

            dgvTuyen.Rows(0).Selected = False
            dgvTuyen.Rows(dgvTuyen.Rows.Count - 2).Selected = True
            Dim tuyen As TuyenDto = LayTuyenTu_DataGridView(dgvTuyen.Rows.Count - 2)
            HienThiThongTinTUYEN(tuyen)
        End If
    End Sub
    Private Sub btnXoaTuyen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaTuyen.Click
        If vtClick = dgvTuyen.Rows.Count - 1 Then
            MessageBox.Show("Chọn dòng có dữ liệu cần xóa!!!", "THÔNG BÁO")
            Exit Sub
        End If
        Dim ma As Integer = dgvTuyen.Rows(vtClick).Cells("MaTuyen").Value

        Dim kq As Integer = TuyenBus.XoaTuyen(ma)
        dgvTuyen.DataSource = TuyenBus.LayDanhSachTuyen()

        Dim tuyen As TuyenDto = LayTuyenTu_DataGridView(0)
        HienThiThongTinTuyen(tuyen)
    End Sub
    Private Sub btnCapNhaTuyen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapNhaTuyen.Click
        Dim ktNhapLieu As String = KT_NhapLieu_Them()
        If (ktNhapLieu <> "") Then
            MessageBox.Show(ktNhapLieu, "THÔNG BÁO")
            Exit Sub
        End If

        Dim kq As Integer = TuyenBus.CapNhatTuyen(Me.LayTuyenTuTextboxs())
        If (kq = 0) Then
            MessageBox.Show("Ten Tuyen đã có trong dữ liệu!!!", "THÔNG BÁO")
        Else
            dgvTuyen.DataSource = TuyenBus.LayDanhSachTuyen()

            dgvTuyen.Rows(0).Selected = False
            dgvTuyen.Rows(vtClick).Selected = True
            Dim tuyen As TuyenDto = LayTuyenTu_DataGridView(vtClick)
            HienThiThongTinTUYEN(tuyen)
        End If
    End Sub


    Private Sub dgvTuyen_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgvTuyen.MouseClick
        vtClick = dgvTuyen.CurrentRow.Index()
        If vtClick = dgvTuyen.Rows.Count - 1 Then
            ResetTextboxs()
            Exit Sub
        End If
        Dim tuyen As TuyenDto = LayTuyenTu_DataGridView(vtClick)
        HienThiThongTinTUYEN(tuyen)
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Close()
    End Sub

End Class